package com.example.view.module.refresh.custom;

import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class RefreshLayout extends RelativeLayout {

    private int top = -80;
    private int h;
    private TextView tvText;
    private boolean isInRefresh;
    private float downY;
    private RefreshListener listener;

    public void setListener(RefreshListener listener) {
        this.listener = listener;
    }

    public RefreshLayout(Context context) {
        super(context);
    }

    public RefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        tvText = new TextView(getContext());
        tvText.setText("下拉刷新");
        tvText.setGravity(Gravity.CENTER);
        tvText.setPadding(0, 30, 0, 30);
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        addView(tvText, params);

        post(new Runnable() {
            @Override
            public void run() {
                h = tvText.getMeasuredHeight();
                tvText.setTranslationY(-h);
            }
        });

    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return getScaleY() < 0;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        super.dispatchTouchEvent(ev);
        if (!isInRefresh) {
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    downY = ev.getY();
                    tvText.setText("下拉刷新");
                    break;
                case MotionEvent.ACTION_MOVE:
                    float d = ev.getY() - downY;
                    scrollTo(0, (int) -d);
                    if (ev.getY() - downY > h + 10) {
                        tvText.setText("释放立即刷新");
                    } else {
                        tvText.setText("下拉刷新");
                    }
                    break;
                case MotionEvent.ACTION_CANCEL:

                    break;
                case MotionEvent.ACTION_UP:
                    if (ev.getY() - downY > h + 10) {
                        smoothTo(-h);
                        tvText.setText("正在刷新");
                        isInRefresh = true;
                        if (listener != null) {
                            listener.onRefresh();
                        }
                    }else {
                        smoothTo(0);
                    }
                    break;
            }
        }
        return true;
    }

    private void smoothTo(int i) {
        ValueAnimator anim = ValueAnimator.ofInt(getScrollY(), i);
        anim.setDuration(300);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                scrollTo(0, (Integer) animation.getAnimatedValue());
            }
        });
        anim.start();
    }

    public void setRefreshFinish(boolean isSuccess) {
        if (isSuccess) {
            tvText.setText("刷新成功");
        } else {
            tvText.setText("刷新失败");
        }
        postDelayed(new Runnable() {
            @Override
            public void run() {
                smoothTo(0);
                isInRefresh = false;
            }
        }, 500);

    }

    public interface RefreshListener {
        void onRefresh();
    }

}
